# -*- coding: utf-8 -*-
'''
@author: olivia.dou
Created on: 2019/5/10 14:05
desc:验证数据
'''

import unittest,logging
import requests
from base.get_config import get_url_dict
from util.db_util import execute_query
from util.date_util import get_datetime_from_now

@unittest.skip("debug")
class testKnewsData(unittest.TestCase):
    def setUp(self):
        self.core_service = get_url_dict()['url4']
        pass

    @unittest.skip("debug other tests")
    def test_dup_article(self):
        """验证在mysql里重复的文章，不会入库到ES"""
        # 查询前一天的重复文章
        str_start_time = get_datetime_from_now(floor_time=True,format="%Y-%m-%d %H:%M:%S",days=-1)
        str_end_time = get_datetime_from_now(floor_time=True,format="%Y-%m-%d %H:%M:%S")

        res = execute_query("select distinct oid from nlp_knews_sentence_all where oid in (select id from nlp_news_feed where dupid is not null and dupid<>'') and crt between '%s' and '%s'"
                      %(str_start_time,str_end_time))

        oids = [item[0] for item in res]

        logging.info("Querying oids...")
        for oid in oids:
            response = requests.get(self.core_service+"/knews/api/v1/es/text/%s"%oid).text
            self.assertTrue(response=='not found')

    def test_es_article(self):
        """验证符合ES入库条件的文章，在ES库中都可以被找到"""
        # 入库条件：dupid为空，有句子status=2且句子company_list不为空
        # 查询前一天的文章
        str_start_time = get_datetime_from_now(floor_time=True,format="%Y-%m-%d %H:%M:%S",days=-1)
        str_end_time = get_datetime_from_now(floor_time=True,format="%Y-%m-%d %H:%M:%S")

        sql = """
            select distinct a.oid from nlp_knews_sentence_all a
            INNER JOIN nlp_knews_sentence_result r
            on a.oid=r.oid
            where a.oid in (select id from nlp_news_feed where dupid is null OR dupid='') and a.crt between '%s' and '%s'
            AND a.status=2 
            and r.company_list is not null and r.company_list<>''
        """%(str_start_time,str_end_time)
        res = execute_query(sql)

        oids = [item[0] for item in res]

        logging.info("Querying oids...")
        for oid in oids:
            response = requests.get(self.core_service+"/knews/api/v1/es/text/%s"%oid)
            self.assertTrue('application/json' in response.headers['Content-Type'],"oid:%s"%oid)
            #1770806


    # 验证句子都有pos字段